import axios from 'axios';
import { appConfig } from '../config/app';
import { Dialog } from '../modal';


export const checkUpdate = (interval = 1000 * 60 * 5, url = '/index.html') => {
  if (!appConfig.env.includes('prod')) return
  let html = ''
  let timer: any = 0
  const fn = async () => {
    const { data } = await axios.get(url)
    if (html) {
      if (data !== html) {
        clearInterval(timer)
        await Dialog.confirm({
          content: '应用有新版本，请问需要更新吗？',
          okText: '刷新页面',
          xClosable: true,
          onOk: () => window.location.reload(),
        })
      }
    } else {
      html = data
    }
  }
  timer = setInterval(fn, interval)
}
